package com.es.common;

import cn.hutool.core.util.StrUtil;
import com.es.util.file.ConfigUtil;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 信息预先配置
 *
 * @author Will
 */
public class PreCache {

    /**
     * 配置文件获取
     */
    public static void setCacheFromConfig() {

    }

    /**
     * 配置文件不存在时执行
     */
    public static void setCacheNoneConfig() {
        if (Cache.getCache().size() > 0) {
            return;
        }

        Object targetClassesPath = ConfigUtil.getDefaultFromProperties("targetClassesPath");
        Object targetJarPath = ConfigUtil.getDefaultFromProperties("targetJarPath");
        Object compileWhenStartApp = ConfigUtil.getDefaultFromProperties("compileWhenStartApp");
        Object compileAndDeploy = ConfigUtil.getDefaultFromProperties("compileAndDeploy");
        Object initStart = ConfigUtil.getDefaultFromProperties("initStart");
        Object threadNum = ConfigUtil.getDefaultFromProperties("threadNum");
        Object preLoadJarPath = ConfigUtil.getDefaultFromProperties("preLoadJarPath");

        Map<String, Object> map = Collections.unmodifiableMap(new ConcurrentHashMap<String, Object>() {{
            put("compileWhenStartApp", compileWhenStartApp == null ? false : booleanMap(compileWhenStartApp));      // 启动程序时执行
            put("targetJarPath", targetJarPath == null ? System.getProperty("user.dir") + "targetJarPathEx/" : targetJarPath);                        // 部署路径
            put("compileAndDeploy", compileAndDeploy == null ? "compile" : compileAndDeploy);                       // 编译并部署
            put("initStart", initStart == null ? true : booleanMap(initStart));                                     // 初始化执行
            put("targetClassesPath", targetClassesPath == null ? System.getProperty("user.dir") + "/" : targetClassesPath);
            put("threadNum", threadNum == null ? 10 : Integer.parseInt(threadNum.toString()));
            put("preLoadJarPath", preLoadJarPath == null ? StrUtil.EMPTY : preLoadJarPath);
        }});

        Cache.setCache(map);
    }

    private static Boolean booleanMap(Object flag) {
        return flag != null && Boolean.parseBoolean(flag.toString());
    }
}
